package com.lcf.email.service;

import cn.hutool.core.collection.CollectionUtil;
import com.lcf.base.exception.SysException;
import com.lcf.common.utils.ThreadUtils;
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

import static com.lcf.base.constant.SysConstant.EMAIL_FROM_NAME;

/**
 * @author lcf
 */
@Slf4j
@Component
public class EmailUtils {
    @Resource
    private JavaMailSender javaMailSender;
    @Resource
    private ThreadUtils threadUtils;
    @Resource
    private MailProperties mailProperties;

    /**
     * 异步发送简单文本邮件
     *
     * @param to      邮箱地址
     * @param subject 主题
     * @param text    内容
     */
    public void sendAsync(String subject, String text, String... to) {
        threadUtils.execute(() -> send(subject, text, null, to));
    }

    /**
     * 异步发送简单文本邮件
     *
     * @param to      邮箱地址
     * @param subject 主题
     * @param text    内容
     */
    public void sendAsync(String subject, String text, List<String> attachList, String... to) {
        threadUtils.execute(() -> send(subject, text, attachList, to));
    }

    /**
     * 异步发送HTML格式邮件
     *
     * @param to          邮箱地址
     * @param subject     主题
     * @param htmlContent 内容
     */
    public void sendHtmlAsync(String subject, String htmlContent, String... to) {
        threadUtils.execute(() -> sendHtml(subject, htmlContent, null, to));
    }

    /**
     * 异步发送HTML格式邮件
     *
     * @param to          邮箱地址
     * @param subject     主题
     * @param htmlContent 内容
     * @param attachList  附件路径
     */
    public void sendHtmlAsync(String subject, String htmlContent, List<String> attachList, String... to) {
        threadUtils.execute(() -> sendHtml(subject, htmlContent, attachList, to));
    }

    /**
     * 同步发送简单文本邮件
     *
     * @param to      邮箱地址
     * @param subject 主题
     * @param text    内容
     */
    public void send(String subject, String text, String... to) {
        send(subject, text, null, to);
    }

    /**
     * 同步发送简单文本邮件
     *
     * @param to      邮箱地址
     * @param subject 主题
     * @param text    内容
     */
    public void send(String subject, String text, List<String> attachList, String... to) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom(mailProperties.getUsername(), EMAIL_FROM_NAME);
            helper.setTo(to);
            helper.setSubject(subject);
            // true表示内容为HTML
            helper.setText(text, false);

            // 添加附件
            if (CollectionUtil.isNotEmpty(attachList)) {
                for (String attach : attachList) {
                    FileSystemResource file = new FileSystemResource(attach);
                    helper.addAttachment(Objects.requireNonNull(file.getFilename()), file);
                }
            }

            javaMailSender.send(message);
        } catch (Exception e) {
            throw new SysException(e);
        }
    }

    /**
     * 同步发送HTML格式邮件
     *
     * @param to          邮箱地址
     * @param subject     主题
     * @param htmlContent 内容
     */
    public void sendHtml(String subject, String htmlContent, String... to) {
        sendHtml(subject, htmlContent, null, to);
    }

    /**
     * 同步发送HTML格式邮件
     *
     * @param to          邮箱地址
     * @param subject     主题
     * @param htmlContent 内容
     * @param attachList  附件路径
     */
    public void sendHtml(String subject, String htmlContent, List<String> attachList, String... to) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom(mailProperties.getUsername(), EMAIL_FROM_NAME);
            helper.setTo(to);
            helper.setSubject(subject);
            // true表示内容为HTML
            helper.setText(htmlContent, true);

            // 添加附件
            if (CollectionUtil.isNotEmpty(attachList)) {
                for (String attach : attachList) {
                    FileSystemResource file = new FileSystemResource(attach);
                    helper.addAttachment(Objects.requireNonNull(file.getFilename()), file);
                }
            }

            javaMailSender.send(message);
        } catch (Exception e) {
            throw new SysException(e);
        }
    }
}
